package com.spa.domain.service;

import cn.hutool.core.util.ObjectUtil;
import com.spa.application.command.admin.CityAddCommand;
import com.spa.application.command.admin.CityConfigTravelCommand;
import com.spa.domain.entity.CitiesServed;
import com.spa.infrastructure.enums.dict.CityStatusEnum;
import com.spa.infrastructure.exception.SpaException;
import com.spa.infrastructure.mapper.CitiesServedMapperStruct;
import com.spa.infrastructure.repository.condition.CitiesServedCondition;
import com.spa.infrastructure.repository.po.CitiesServedPO;
import com.spa.infrastructure.repository.service.CitiesServedRepositoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 开通城市管理(CitiesServed)服务实现类
 *
 * @author makejava
 * @since 2024-01-12 15:52:58
 */
@Service
public class CitiesServedService {

    @Resource
    private CitiesServedRepositoryService citiesServedRepositoryService;

    public CitiesServed getById(long id) {
        CitiesServedPO citiesServedPO = citiesServedRepositoryService.queryById(id);
        return CitiesServedMapperStruct.INSTANCE.po2Entity(citiesServedPO);
    }

    public List<CitiesServed> listInfo(CitiesServedCondition condition) {
        List<CitiesServedPO> list = citiesServedRepositoryService.listInfo(condition);
        return CitiesServedMapperStruct.INSTANCE.po2Entity4List(list);
    }

    public void add(CityAddCommand command) {
        if (ObjectUtil.isNotNull(command.getRegion()) && ObjectUtil.isNotNull(command.getCityName())){
            if (citiesServedRepositoryService.existsCity(CitiesServedMapperStruct.INSTANCE.command2Condition(command))) {
                throw new SpaException("城市已存在");
            }
            citiesServedRepositoryService.add(CitiesServedMapperStruct.INSTANCE.command2Po(command));
        }

    }

    public void open(Long id) {
        CitiesServedPO po = new CitiesServedPO();
        po.setId(id);
        po.setStatus(CityStatusEnum.$10.getCode());
        citiesServedRepositoryService.updateCity(po);
    }

    public void close(Long id) {
        CitiesServedPO po = new CitiesServedPO();
        po.setId(id);
        po.setStatus(CityStatusEnum.$20.getCode());
        citiesServedRepositoryService.updateCity(po);
    }

    /**
     * 配置城市对应的出行费基础配置
     * @param command
     */
    public void travelConfig(CityConfigTravelCommand command) {
        CitiesServedPO po = CitiesServedMapperStruct.INSTANCE.command2Po(command);
        citiesServedRepositoryService.updateCity(po);
    }
}
